#include "UI.h"

#include <imgui/imgui.h>
#include <imgui/imgui_impl_opengl3.h>
#include <imgui/imgui_impl_sdl.h>
#include "glm/gtc/type_ptr.hpp"
#include "Singleton.h"
#include "GlobalData.h"
#include "glad/glad.h"

class UI_Impl
{
public:
    bool showRobotParmWindow = false;
    bool showEnvParmWindow = false;
    std::vector<std::string> robotLinkColorText;
    std::vector<std::string> robotLinkJointText;

    UI_Impl()
    {
        robotLinkColorText.clear();
        auto size = Singleton<GlobalData>::Instance().RobotLinkColors.size();
        robotLinkColorText.reserve(size);
        for (int i = 0; i < size; ++i)
        {
            robotLinkColorText.emplace_back(std::string("机器人连杆色彩 ") + std::to_string(i));
        }
        robotLinkJointText.reserve(6);
        for (int i = 1; i < 7; ++i)
        {
            robotLinkJointText.emplace_back(std::string("关节") + std::to_string(i));
        }
    }

    void ShowAppMainMenuBar()
    {
        if (ImGui::BeginMainMenuBar())
        {
            if (ImGui::BeginMenu("文件"))
            {
                if (ImGui::MenuItem("退出", "ESC"))
                {
                    Singleton<GlobalData>::Instance().RunFlag = false;
                }
                ImGui::EndMenu();
            }
            if (ImGui::BeginMenu("窗口"))
            {
                ImGui::MenuItem("机械臂属性", nullptr, &showRobotParmWindow);
                ImGui::MenuItem("环境参数", nullptr, &showEnvParmWindow);
                ImGui::Separator();
                if (ImGui::MenuItem("Cut", "CTRL+X")) {}
                if (ImGui::MenuItem("Copy", "CTRL+C")) {}
                if (ImGui::MenuItem("Paste", "CTRL+V")) {}
                ImGui::EndMenu();
            }
            ImGui::EndMainMenuBar();
        }
    }

    void ShowRobotParmWindow()
    {
        ImGui::Begin("机械臂属性", &showRobotParmWindow, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize);
        ImGui::Text("在此处可以设置机械臂的属性");
        auto& robotLinkColors = Singleton<GlobalData>::Instance().RobotLinkColors;

        for (int i = 0; i < robotLinkColors.size(); ++i)
            ImGui::ColorEdit3(robotLinkColorText[i].c_str(), glm::value_ptr(robotLinkColors[i]));
        ImGui::End();
    }

    void ShowEnvParmWindow()
    {
        ImGui::Begin("环境参数设置", &showEnvParmWindow, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize);
        ImGui::ColorEdit3("背景颜色", glm::value_ptr(Singleton<GlobalData>::Instance().RenderBackgroundColor));
        ImGui::Separator();
        ImGui::Checkbox("绘制天空盒", &(Singleton<GlobalData>::Instance().DrawSkybox));
        ImGui::Separator();
        ImGui::Checkbox("绘制坐标系", &(Singleton<GlobalData>::Instance().DrawAxis));
        ImGui::Separator();
        ImGui::Checkbox("绘制地面", &(Singleton<GlobalData>::Instance().DrawGround));
        ImGui::Separator();
        ImGui::ColorEdit3("光照颜色", glm::value_ptr(Singleton<GlobalData>::Instance().EnvLightColor));
        ImGui::End();
    }

    void ShowRobotJointParmWindow()
    {
        auto& jointAngle = Singleton<GlobalData>::Instance().JointAngle;
        auto& jointAngleMax = Singleton<GlobalData>::Instance().JointAngleMax;
        auto& jointAngleMin = Singleton<GlobalData>::Instance().JointAngleMin;
        auto& jointAngleDirty = Singleton<GlobalData>::Instance().JointAngleDirty;
        ImGui::SetNextWindowPos({10, 35});
        ImGui::Begin("设置关节参数", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove);
        for (int i = 1; i < 7; ++i)
            if (ImGui::SliderFloat(robotLinkJointText[i - 1].c_str(), &jointAngle[i], jointAngleMin[i], jointAngleMax[i]))
                jointAngleDirty = true;
        ImGui::End();
    }

    void UILogic()
    {
        ShowAppMainMenuBar();
        ShowRobotJointParmWindow();
        if (showRobotParmWindow)
            ShowRobotParmWindow();
        if (showEnvParmWindow)
            ShowEnvParmWindow();
    }
};


UI::UI(SDL_Window* w, SDL_GLContext& glContext)
{
    window = w;
    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    io = &ImGui::GetIO();
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsClassic();

    // Setup Platform/Renderer backends
    ImGui_ImplSDL2_InitForOpenGL(window, glContext);
    ImGui_ImplOpenGL3_Init("#version 330 core");

    ImFont* font = io->Fonts->AddFontFromFileTTF("bits/Alibaba-PuHuiTi-Regular.ttf", 20.0f, nullptr, io->Fonts->GetGlyphRangesChineseSimplifiedCommon());
    IM_ASSERT(font != nullptr);

    uiImpl = new UI_Impl();
}

UI::~UI()
{
    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplSDL2_Shutdown();
    ImGui::DestroyContext();
    delete uiImpl;
}

void UI::Logic()
{
    // Start the Dear ImGui frame
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplSDL2_NewFrame(window);
    ImGui::NewFrame();

    uiImpl->UILogic();

    // Rendering
    ImGui::Render();
    glViewport(0, 0, (int)io->DisplaySize.x, (int)io->DisplaySize.y);
}

void UI::HandleInput(SDL_Event &e)
{
    ImGui_ImplSDL2_ProcessEvent(&e);
}

void UI::Render()
{
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
